﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1.work3
{
    class ClockEventArgs : EventArgs
    {
        public DateTime CurrentTime { get; }
        public ClockEventArgs(DateTime currentTime)
        {
            CurrentTime = currentTime;
        }
    }

    class Clock
    {

        public event EventHandler<ClockEventArgs> Tick;
        public event EventHandler<ClockEventArgs> Alarm;

        public DateTime AlarmTime { get; set; }

        private bool _alarmRang = false;


        public void Start()
        {
            while (true)
            {
                Thread.Sleep(1000);  
                DateTime now = DateTime.Now;

                OnTick(now);

                if (!_alarmRang && now >= AlarmTime)
                {
                    _alarmRang = true;
                    OnAlarm(now);
                }
            }
        }

        protected virtual void OnTick(DateTime time)
        {
            Tick?.Invoke(this, new ClockEventArgs(time));
        }

        protected virtual void OnAlarm(DateTime time)
        {
            Alarm?.Invoke(this, new ClockEventArgs(time));
        }
    }

    class work35
    {
        static void Main()
        {
            Console.Write("请输入闹钟时间（时:分:秒，如 17:30:00）：");
            if (!TimeSpan.TryParse(Console.ReadLine(), out TimeSpan ts))
            {
                Console.WriteLine("格式错误，程序退出。");
                return;
            }

            DateTime alarmTime = DateTime.Today + ts;
            if (alarmTime <= DateTime.Now)
                alarmTime = alarmTime.AddDays(1);

            Clock clock = new Clock
            {
                AlarmTime = alarmTime
            };


            clock.Tick += (s, e) =>
            {
                Console.WriteLine($"嘀嗒... 当前时间：{e.CurrentTime:HH:mm:ss}");
            };


            clock.Alarm += (s, e) =>
            {
                Console.WriteLine($"！！！ 闹钟响了！！！ 现在时间：{e.CurrentTime:HH:mm:ss}");
            };

            Console.WriteLine($"\n闹钟已设置：{alarmTime:yyyy-MM-dd HH:mm:ss}，开始计时...\n");
            clock.Start();
        }
    }
}
